<template>
  <el-dialog v-if="load" :close-on-click-modal="false" title="备货参数设置" v-model="visible" append-to-body width="800px" :z-index="2000" @close="handleClose">
    <el-form label-position="top">
      <div class="grid grid-cols-2 gap-x-3">
        <el-form-item label="备货天数">
          <el-input-number v-model="form.data.demand_day" controls-position="right" :min="0" :step="1"
            :precision="2" class="w-full" />
        </el-form-item>
        <!-- <el-form-item label="采购天数">
          <el-input-number v-model="form.data.purchase_day" controls-position="right" :min="0" :step="1"
            :precision="2" class="w-full" />
        </el-form-item>
        <el-form-item label="运输天数">
          <el-input-number v-model="form.data.transit_day" controls-position="right" :min="0" :step="1"
            :precision="2" class="w-full" />
        </el-form-item> -->
        <el-form-item label="安全天数">
          <el-input-number v-model="form.data.safe_day" controls-position="right" :min="0" :step="1" :precision="2"
            class="w-full" />
        </el-form-item>
        <el-form-item label="本地仓处理天数">
          <el-input-number v-model="form.data.middle_process_day" controls-position="right" :min="0" :step="1"
            :precision="2" class="w-full" />
        </el-form-item>
        <el-form-item label="海外上架天数">
          <el-input-number v-model="form.data.oversea_shelve_day" controls-position="right" :min="0" :step="1"
            :precision="2" class="w-full" />
        </el-form-item>
        <el-form-item label="采购频次">
          <el-input-number v-model="form.data.purchase_frequency" controls-position="right" :min="0" :step="1"
            :precision="2" class="w-full" />
        </el-form-item>
      </div>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'

const api = inject('api')
const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({data: {}})
const type = inject('type')

const emit = defineEmits(['close', 'success'])

// 打开弹窗
const open = async (selection) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  const is_ids = selection.map(d => d.is_id)
  const keys = ['demand_day', 'safe_day', 'middle_process_day', 'oversea_shelve_day', 'purchase_frequency']
  const item = selection[0]
  const data = keys.reduce((acc, cur) => ((acc[cur] = selection.length === 1 ? item[cur] : undefined), acc) ,{})
  loadings = reactive({})
  visible.value = true
  form.value = {is_ids, data, type}
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = () => {
  loadings.submit = true
  api.updateDemandRule(form.value).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>